<link rel="stylesheet" type="text/css" href="css/page.css"/>
<style type="text/css">
button{cursor: pointer;}
.wrap{width:80%;float:right;}
table{width:100%; line-height:23px;margin:10px 0; border-color:#d5dce2;}
th,td{ text-align:center; border-color:#d5dce2; line-height:30px;}
th{background:#f2f9ff;}
.pri{width:109px;height:22px;text-align:center;}
.fenyeiWrap{width:85%;}
#tplid{display:none;}
.addcp {background:url("http://www.12301.cc/images/btn23_two1.png") no-repeat scroll left top;display: inline-block;height: 23px;padding-left: 8px;text-decoration: none;}
.addcp span {background:url("http://www.12301.cc/images/btn23_two1.png") no-repeat scroll right top;color: #FFFFFF;display: inline-block;font-size: 12px;font-weight: bold;height: 23px;line-height: 23px;padding-right: 8px;}
.ty{background:#f0851d; border:1px solid #a9550f; height:25px;width:75px; color:#fff; padding-bottom:2px;  padding-bottom:0px\9; }
.tpllist{float:right;line-height: 25px;}
.txtlf{ text-align:left; padding-left:15px;}
.add,.rude {display: block;width: 10px;height: 11px;border: 1px solid;line-height: 9px;
line-height: 8px\9;padding-right: 1px;font-size: 15px;cursor: pointer;}
.add {position: absolute;right: -20px;top: 9px;}
.rude{position: absolute;right: 45px;top: 9px;}
</style>

<div class="wrap">
<h3 class="mc_title">
    
    <span class="mctit_1"><span class="mctit_2 ">自供应产品</span></span>
    <span class="mctit_1 mctit_3"><a href="priceset_sale.html?did=<?=$did?>" class="mctit_2 mctit_4">转分销产品</a></span>
</h3></br>
    <iframe name="ifr" style="display:none;"></iframe>
  <form action="priceset_dev.php" target="ifr" method="post">
    <span style="font-size:14px;font-weight:bold;color:#2A98DA;margin-right:10px;vertical-align: -2px;"><?=$dname?></span>
    <select name="tplid" id="tplid" onchange="xzmb()">
        <option value="">-请选择模板-</option>
        <option value="-1">--新增模板--</option>
    </select>
    <button type="button" onclick="ty()" class="ty">套用模板</button>
    <font color="red">您可以选择套用模板来快速配置结算价。</font>
    <span style="font-size:14px;color:#2A98DA;">
        产品搜索: <input type="text" name="prodname" id="prodname" value="<?=$prodname?>" />
    </span>
    <input type="button" id="btn" style="width:60px" value="搜索">
    <a class="tpllist" onclick="history.go(-1)" href="javascript:void(0);">返回>></a>
    <input type="hidden" name="did" id="did" value="<?=$did?>" />
    <input type="hidden" name="bind" id="bind" value="<?=($tplid?1:0)?>" />
      <input type="hidden" id="now" value="<?=$tplid?>" />
      <input name="pdata" value="<?=base64_encode(json_encode($sec))?>" type="hidden"/>
    <table border="1" id="listTable">
        <thead>
            <tr>
                <!-- <th>购买权限</th> -->
                <th>
                    <a id="selectAllBtn" class="ckall" href="javascript:;">全选</a>
                    <a id="cannelAllBtn" class="reck" href="javascript:;">取消</a>
                </th>
                <th>产品名称</th>
                <th>供货价</th>
                <th>
                    <div style="position:relative; display:inline-block;" >
                        <span onclick="chPrice(this);" class="rude">-</span>
                        <span class="p_t">结算价</span>
                        <span onclick="chPrice(this);" class="add">+</span>
                    </div>
                </th>
            </tr>
        </thead>
        <tbody>
        <?foreach($prod_list as $p):?>
            <?
            if (in_array($p['id'], $sec)) {
                $pri        = $op[$p['id']]/100;
                $disabled   = "";
                $checked    = "checked";
            }
            else{
                $pri        = 0;
                $checked    = "";
                $disabled   = "disabled";
            }
            ?>
            <tr>
                <td>
                    <input type="checkbox"
                           name="pid_list[<?=$p['id']?>]"
                           class="tids"
                           value="<?=$p['lid']?>"
                           <?=$checked?>
                        />
                </td>
                <td class="txtlf"><?=$p['p_name']?></td>
                <td><?=$p['fp']?></td>
                <td>
                    <input class="pri"
                           type="text"
                           name="pri_list[<?=$p['id']?>]"
                           value="<?=$pri+$p['fp']?>"
                           <?=$disabled?>
                        />
                </td>
            </tr>
        <?endforeach;?>
        </tbody>
    </table>
    <a id="saveBtn" class="addcp" href="javascript:void(0);"><span>保存价格</span></a>
  </form>
  <?page($total,$currentPage,$pageSize);?>
</div>
<script type="text/javascript">
    function tyy(){
        //if($("#tplid").css("display")=="none"){
        $.getJSON("call/hasTpl.php",function(tpl){
            if(tpl){
                $("#tplid").show();
                var now=$("#now").val();
                for(var i=0;i<tpl.length;i++){
                    $("#tplid").append("<option "+(tpl[i]['id']==now?"selected":"")+" value='"+tpl[i]['id']+"'>"+tpl[i]['title']+"</option>");
                }
            }
            else{
                if(confirm('检测到您尚未创建过模板,是否创建?')){
                    //    window.open('addtpl.html','_blank');
                    window.location='addtpl.html';
                }
            }
        })
        //}
    }
    tyy();

    function ty(){
        if($("#tplid").css("display")=="none"){
            $.getJSON("call/hasTpl.php",function(tpl){
                if(tpl){
                    $("#tplid").show();
                    var now=$("#now").val();
                    for(var i=0;i<tpl.length;i++){
                        $("#tplid").append("<option "+(tpl[i]['id']==now?"selected":"")+" value='"+tpl[i]['id']+"'>"+tpl[i]['title']+"</option>");
                    }
                }
                else{
                    if(confirm('检测到您尚未创建过模板,是否创建?')){
                        //    window.open('addtpl.html','_blank');
                        window.location='addtpl.html';
                    }
                }
            })
        }
        else{
            if($("#tplid").val())$("#bind").val(1);
            document.forms[0].submit();
        }
    }
    function xzmb(){
        var t=$('#tplid').val();
        if(t>0){
            $("#bind").val(0);
            $.getJSON("call/getTpl.php",{tplid:t},function(tpl){
                $(":checkbox").each(function(){
                    var ckbox=$(this);
                    if(ckbox.val()!="all"){
                        if(!isNaN(tpl[ckbox.val()])){
                            ckbox.attr("checked",true).parent().parent().find(":text").attr("disabled",false).val(($("#stype").length && $("#stype").attr("checked") ? 0 :Number(ckbox.parent().parent().children().eq(2).html()))+Number(tpl[ckbox.val()]));
                        }
                        else{
                            ckbox.attr("checked",false).parent().parent().find(":text").attr("disabled",true);
                        }
                    }
                });
            });
        }
        else if(t==-1){
            window.location='addtpl.html';
        }
    }
    function chPri(){
        $("#bind").val(0);
        if($("#tplid").val()){
            if(confirm('是否将该分销商与所选模板绑定,绑定后可通过修改模板修改该分销商的配置?')){
                $("#bind").val(1);
            }
        }
        document.forms[0].submit();
    }

    function chPrice(object){
        var action = 0;
        if($(object).attr("class")=="add") action = 1;
        $(".pri").each(function(i,ele){
            if($(this).attr("disabled")!="disabled"){
                var price = $(this).val();
                var supply = Number($(this).parents().find(".supply").html());
                if(price=='') price = supply;
                if(action==1){// plus
                    price = Number(price) + 1;
                }else{
                    price = Number(price) - 1;
                    // alert(price);
                    if(price<supply) price = supply;
                }

                price = price.toFixed(2);
                $(this).val(price);
            }
        });
    }

    $(document).ready(function(){
        $("#saveBtn").on('click', function(){
            $("#bind").val(0);
            if($("#tplid").val()){
                if(confirm('是否将该分销商与所选模板绑定,绑定后可通过修改模板修改该分销商的配置?')){
                    $("#bind").val(1);
                }
            }
            document.forms[0].submit();
        });
        //checkbox 选中/取消选中处理
        $("#listTable").on('change', '.tids', function(){
            $(this).parent().parent().find('.pri').attr('disabled',!this.checked);
        });
        //全选
        $("#selectAllBtn").on("click",function(e){
            selectAll();
        })
        //全不选
        $("#cannelAllBtn").on("click",function(e){
            cancelAll();
        })
        //搜索
        $("#btn").click(function(){
            var did=$("#did").val();
            var prodname= encodeURIComponent($("#prodname").val());
            window.location="priceset_dev.html?did="+did+"&prodname="+prodname;
        })
    });

    var selectAll = function(){
        $("#listTable").find(".tids").each(function(index,inp){
            $(inp).prop("checked","checked");
            $(this).parent().parent().find(':text').attr('disabled',!this.checked);
        })
    };
    var cancelAll = function(){
        $("#listTable").find(".tids").each(function(index,inp){
            $(inp).prop("checked","");
            $(this).parent().parent().find(':text').attr('disabled',!this.checked);
        })
    };
</script>